package com.sxu.demo.mapper;


import com.sxu.demo.model.UserInfo;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper  {

    @ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoByDelete(Integer deleteFlag);

    @Select("select * from userinfo where delete_flag = ${deleteFlag}")
    List<UserInfo> getUserInfoByDelete2(Integer deleteFlag);

    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfo2(@Param("flag") Integer deleteFlag, Integer gender);

    //增：insert
    @Options(useGeneratedKeys = true , keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into userinfo (username,password,age,gender) " +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password},age = #{age} ,gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

    //起别名
    @Select("select id,username,password,age,gender,phone, " +
            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime " +
            "from userinfo")
    List<UserInfo> selectUserInfos();

    //结果映射
    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select id,username,password,age,gender,phone, " +
            "delete_flag,create_time,update_time " +
            "from `userinfo`")
    List<UserInfo> selectUserInfos2();

    //驼峰自动转换
    @Select("select id,username,password,age,gender,phone, " +
            "delete_flag,create_time,update_time " +
            "from `userinfo`")
    List<UserInfo> selectUserInfos3();

    @Select("select * from userinfo where username = #{userName}")
    UserInfo getUserByName(String userName);

    @Select("select * from userinfo where username = '${userName}'")
    UserInfo getUserByName2(String userName);

    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserInfoByOrder(String order);

}


